package com.jl.cloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.jl.cloud.commom.aspect.DistributedLock;
import com.jl.cloud.commom.base.BaseResponse;
import com.jl.cloud.consumer.model.Terminal;
import com.jl.cloud.consumer.service.TerminalService;
import com.jl.cloud.consumer.service.kafka.KafkaProducer;
import com.jl.cloud.terminal.api.client.TerminalClient;
import com.jl.cloud.terminal.api.dto.TerminalResDto;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RefreshScope
public class ConsumerController {

    @Resource
    private TerminalService terminalService;

    @Resource
    private TerminalClient terminalClient;

    @Value("${test:1}")
    private Integer t;

    @Resource
    private KafkaProducer kafkaProducer;

    @GetMapping("getTerminal")
    @DistributedLock
    public BaseResponse<List<Terminal>> getTerminal() {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setMessage("成功");
        List<Terminal> all = terminalService.findAll();
        kafkaProducer.send(JSONObject.toJSONString(all));
        baseResponse.setResult(all);
        return baseResponse;
    }


    @GetMapping("getTerminalUrl")
    public BaseResponse<List<TerminalResDto>> getTerminalUrl() {
        return terminalClient.getTerminal();
    }


}
